package com.ecube.test.control;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.PvFunctionEnum;
import com.ecube.protocol.pv.PvPowerAdjustSetResp;
import com.ecube.utils.EnumUtils;
import lombok.Builder;
import lombok.Data;

/**
 * 光伏
 *  功率调节设置
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/6 19:35
 */
public class SendPvControl {

    public static void main(String[] args) {
        sendPowerAdjustSetResp("FAT", "100080001", "12025804", PvInfo.builder().effect(2).result(1).index(56).build());
        sendPowerAdjustSetResp("FAT", "100080001", "12025805", PvInfo.builder().effect(2).result(1).index(55).build());
    }

    /**
     * 功率调节回复
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     * @param info info
     */
    private static void sendPowerAdjustSetResp(String env, String productCode, String deviceCode, PvInfo info){
        PvFunctionEnum function = (PvFunctionEnum) EnumUtils.getFunction(PvPowerAdjustSetResp.class);
        if(function == null){
            return;
        }
        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        PvPowerAdjustSetResp adjustSetResp = new PvPowerAdjustSetResp();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V1.0.1");
        reportHand.setIndex(info.getIndex());
        reportStandard.setHeader(reportHand);

        // 设置储能信息
        adjustSetResp.setEffect(info.getEffect());
        adjustSetResp.setResult(info.getResult());
        adjustSetResp.setReason(info.getReason());
        reportStandard.setDataBody(adjustSetResp);

        String reportContent = JSON.toJSONString(reportStandard);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }


    @Data
    @Builder
    private static class PvInfo {
        /**
         * 结果：1-成功；2-失败
         */
        private Integer result = 1;

        /**
         * 生效类型：1-立即生效；2-取消生效；
         */
        private Integer effect;

        /**
         * 失败原因: 0-功能不可用；1-参数错误；
         */
        private Integer reason;

        /**
         * 序号
         */
        private Integer index;
    }
}
